package nl.rainbowsheep.reisorg.empl.site.service;

import java.util.ArrayList;
import java.util.List;
import nl.rainbowsheep.reisorg.empl.site.authentication.BasicAuthInterceptor;
import nl.rainbowsheep.reisorg.empl.site.model.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class RegionService {

    @Value("${api.url}")
    String url;

    public List<Region> getAll() {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        return template.getForObject(url + "region/getall?" + getLocaleParam(), List.class);
    }

    public Region getByRegionId(long id) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        Region region = template.getForObject(url + "/region/getregionbyid?id=" + id + "&" + getLocaleParam(), Region.class);
        return region;
    }
    
    public Region getById(long id) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        Region region = template.getForObject(url + "/region/getregion?id=" + id + "&" + getLocaleParam(), Region.class);
        return region;
    }
    
    public List<Region> getByCountryId(Long id) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        return template.getForObject(url + "/region/getbycountryid?id=" + id + "&" + getLocaleParam(), List.class);
    }
    

    public Region insertRegion(Region region) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        return template.postForObject(url + "region/save", region, Region.class);
    }
    
    /**
     * Get current locale (from cookie)
     *
     * @return string with locale param
     */
    private String getLocaleParam() {
        return "locale=" + LocaleContextHolder.getLocale().getLanguage();
    }

}
